package office.service.hr.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.hr.form.MatterSpecifyConditionForm;
import office.action.hr.form.MatterSpecifyForm;
import office.action.hr.wrapper.MatterSpecifyWrapper;
import office.bo.hr.impl.MatterSpecify;
import office.domainset.BizDomainProviderSetImpl;
import office.service.hr.IMatterSpecifyService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class MatterSpecifyServiceImpl extends BaseBizServiceImpl implements IMatterSpecifyService{
	
	public void editMatterSpecify(MatterSpecifyForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			MatterSpecify bo = (MatterSpecify)domainProviderSetImpl.getMatterSpecifyDP().newEmptyObj();
			bo =(MatterSpecify) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑其它事项指定交班人失败!", e);
		}
	}
	
	public Page getPageInfo(MatterSpecifyConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getMatterSpecifyDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "MatterSpecify.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getTitle())){
				condMap.put("title", condition.getTitle());
			}
			if(null!=condition&&!Util.isNull(condition.getHandleUser())){
				condMap.put("handleUser", condition.getHandleUser());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findMatterSpecifyByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<MatterSpecifyWrapper> findAllMatterSpecify(MatterSpecifyConditionForm condition,Page page)throws BusinessException{
		List<MatterSpecifyWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getMatterSpecifyDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getTitle())){
				condMap.put("title", condition.getTitle());
			}
			if(null!=condition&&!Util.isNull(condition.getHandleUser())){
				condMap.put("handleUser", condition.getHandleUser());
			}
			List list=new ArrayList<MatterSpecify>();
			if(null==page){
				list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findMatterSpecifyByIdAndName",condMap,new HashMap());
			}else{
				list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findMatterSpecifyByIdAndName",condMap,new HashMap(),page);
			}
			MatterSpecifyWrapper wrapper;
			for(int i=0;i<list.size();i++){
				MatterSpecify bo = (MatterSpecify)list.get(i);
				wrapper = new MatterSpecifyWrapper();
				wrapper.copyToWrapper(bo);
				//处理人
				wrapper.setHandleUserName(bo.getUser(domainProviderSetImpl,bo.getHandleUser()).getCname());
				//创建人
				wrapper.setCreateManName(bo.getUser(domainProviderSetImpl,bo.getCreateMan()).getCname());
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取其它事项指定交班人列表失败！", e);
		}
		return wrapperList;
	}

	public void delMatterSpecifyById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getMatterSpecifyDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除其它事项指定交班人失败！", e);
		}
	}
	
	public MatterSpecifyForm getById(String id)throws BusinessException{
		MatterSpecifyForm form = new MatterSpecifyForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			MatterSpecify bo = (MatterSpecify)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getMatterSpecifyDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取其它事项指定交班人失败!", e);
		}
		return form;
	}
	public void handleMatter(String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			MatterSpecify bo = (MatterSpecify)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getMatterSpecifyDP(), id);
			bo.setHandleState("2");
			bo.store(domainProviderSetImpl);
			
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("对待办处理事项处理失败!", e);
		}
	}
}
